const express = require("express");
const route = express.Router()
const path = require("path")
//引入权限校验中间件
const isLoginMiddle = require("../middleware/isLoginMiddle")

//引入数据库集合
const advModel = require("../db/modle/advModel")


//查询广告接口
route.get("/advList/:pageNo/:everyNum", isLoginMiddle, async (req, res) => {
  const {
    pageNo,
    everyNum
  } = req.params;

  try {
    const findResult = await advModel.find({}).limit(everyNum).skip((pageNo - 1) * 3);
    //请求总数量
    const allAdvCount = await advModel.count({})
    return res.send({
      code: 10000,
      msg: '查询成功',
      data: {
        advList: findResult,
        pageCount: Math.ceil(allAdvCount / everyNum)
      }
    })
  } catch (e) {
    console.log('查询异常' + e.message);
    res.send({
      data: 10004,
      msg: '查询失败' + e.message,
      data: null
    })
  }
})

module.exports = route;